package com.yookey.config;

//swagger2的配置文件，在项目的启动类的同级文件建立

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.yookey.common.utils.JWTUtil;
import com.yookey.model.dto.AccountDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.StringRedisTemplate;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Profile({"local","dev", "test","prod" })
public class SwaggerConfiguration {
    @Autowired
    private StringRedisTemplate template;

    @Bean
    public Docket createRestCardApi() {
        return createDocker("中越demo","com.yookey.controller",getParamterList());
    }



    private Docket createDocker(String groupName,String basePackage,List<Parameter> ps) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName)
                .apiInfo(createDegaultApiInfo())
                .globalOperationParameters(ps)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any()) //过滤的接口
                .build();
    }

    private ApiInfo createDegaultApiInfo() {
        return createApiInfo("中越demo","中越demo","1.0");
    }

    private ApiInfo createApiInfo(String title,String description,String version) {
        return new ApiInfoBuilder()
                .title(title) //大标题
                .description(description) //详细描述
                .contact(new Contact("江苏中越", "", "")) //作者
                .version(version)
                .build();
    }

    private List<Parameter> getParamterList(){
        List<Parameter> aParameters = new ArrayList<Parameter>();
        aParameters.add(setHeaderToken());
        return aParameters;
    }

    private Parameter setHeaderToken() {
//        AccountInfo accountInfo = new AccountInfo();
//        accountInfo.setAccountId("8");
//        accountInfo.setAccountNo("13404243539");
//        accountInfo.setRealName("张三");
//        accountInfo.setCompanyId("17");
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setId(66);
        accountDTO.setUsername("sadfa");
        String token = JWTUtil.generateToken(accountDTO);
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder.name("token").defaultValue(token)
                .description("jwt测试用").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        return aParameterBuilder.build();
    }
}
